package edu.software.PBL_Management_System.service.impl;

import edu.software.PBL_Management_System.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
@Slf4j
//邮件服务
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    //用户登录发送验证码邮件

    /**
     *
     * @param to 收件人邮箱
     * @param code 验证码
     * @return
     */
    @Override
    public boolean sendVerificationCode(String to, String code) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setTo(to);
            message.setSubject("登录验证码");
            message.setText("您的验证码是：" + code + "，有效期5分钟，请勿泄露给他人。");
            mailSender.send(message);
            log.info("验证码邮件已发送至：{}", to);
            return true;
        } catch (Exception e) {
            log.error("发送验证码邮件失败：{}", e.getMessage());
            return false;
        }
    }
}